package com.mike.bdb.ui.provider.tree;

import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MikeTreeContentProvider implements ITreeContentProvider {

	private static final Logger LOG = LoggerFactory.getLogger(MikeTreeContentProvider.class);
	
	@Override
	public Object[] getChildren(Object obj) {
		if(obj instanceof TreeNode) {
			TreeNode root = (TreeNode) obj;
			return root.isLeaf() ? null : root.getChild().toArray(); 
		}
		return null;
	}

	@Override
	public Object getParent(Object obj) {
		if(obj instanceof TreeNode) {
			TreeNode root = (TreeNode) obj;
			return root.getParent();
		}
		return null;
	}

	@Override
	public boolean hasChildren(Object obj) {
		if(obj instanceof TreeNode) {
			TreeNode root = (TreeNode) obj;
			return !root.isLeaf();
		}
		return false;
	}

	@Override
	public Object[] getElements(Object obj) {
		Object[] result = null;
		if(obj instanceof Set<?>) {
			Set<?> nodes = (Set<?>) obj;
			result = nodes.toArray(new Object[0]);
		}
		if(LOG.isDebugEnabled()) {
			LOG.debug(ArrayUtils.toString(result));
		}
		return result;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
	}

	
}
